package com.dycx.cms.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dycx.cms.mapper.ArticleRecommendMapper;
import com.dycx.cms.dto.ArticleRecommendDTO;
import com.dycx.cms.entity.ArticleRecommendDO;
import com.dycx.cms.service.ArticleRecommendService;
import com.dycx.cms.vo.ArticleRecommendVO;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 推荐类型(ArticleRecommend)表服务实现类
 *
 * @author wuzongbin
 * @since 2023-05-25 10:47:28
 */

@Service
@AllArgsConstructor
public class ArticleRecommendServiceImpl extends ServiceImpl<ArticleRecommendMapper, ArticleRecommendDO> implements ArticleRecommendService {

    private ArticleRecommendMapper articleRecommendMapper;


    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Integer add(ArticleRecommendDTO dto) {
        ArticleRecommendDO entity = BeanUtil.toBean(dto, ArticleRecommendDO.class);
        if (this.save(entity)) return entity.getId();

        return 0;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean edit(ArticleRecommendDTO dto) {
        return this.updateById(BeanUtil.toBean(dto, ArticleRecommendDO.class));
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ArticleRecommendVO getInfo(Integer id) {
        ArticleRecommendDO entity = this.getById(id);
        return BeanUtil.toBean(entity, ArticleRecommendVO.class);
    }

    /**
     * 分页查询
     *
     * @param dto 实例对象
     * @return 查询结果
     */
    @Override
    public Page<ArticleRecommendVO> getPage(ArticleRecommendDTO dto) {
        Page<ArticleRecommendVO> page = new Page<>(dto.getPageIndex(), dto.getPageSize());
        return this.articleRecommendMapper.getPage(page, dto);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean delete(Integer id) {
        return this.removeById(id);
    }

    @Override
    public Boolean delete(ArticleRecommendDTO dto) {
        LambdaQueryWrapper<ArticleRecommendDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleRecommendDO::getArticleId, dto.getArticleId());
        wrapper.eq(ArticleRecommendDO::getRecommendTypeId, dto.getRecommendTypeId());

        return this.remove(wrapper);
    }


    /**
     * 获取最大排序
     *
     * @return 查询结果
     */
    @Override
    public Integer getMaxSort() {
        return this.articleRecommendMapper.getMaxSort();
    }
}

